package com.moyq5.permit.center.feign.server;

import java.util.Properties;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * 事务配置
 * @author Moyq5
 * @date 2018年8月7日
 */
@Configuration
@Aspect
public class TransConfiguration {
	
	private static final Properties pros = new Properties();
	
	static {
		pros.setProperty("check", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("persist", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("merge", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("insert*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("edit*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("remove*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("modify*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("move*", "PROPAGATION_REQUIRED,-Exception");
        pros.setProperty("*", "PROPAGATION_REQUIRED,-Exception,readOnly");
	}
	
	@Bean
    public TransactionInterceptor txAdvice(PlatformTransactionManager transactionManager) {
        return new TransactionInterceptor(transactionManager, pros);
    }
	
	@Bean
    public DefaultPointcutAdvisor advisor0(TransactionInterceptor txAdvice){
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* com.moyq5.permit.center.core.service.*.*(..))");
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(txAdvice);
        return advisor;
    }
	
	@Bean
    public DefaultPointcutAdvisor advisor2(TransactionInterceptor txAdvice){
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* com.moyq5.mvc.framework.dao.Service.*(..))");
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(txAdvice);
        return advisor;
    }
 
}
